package com.cn.ant.modules.score.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.modules.score.dao.LqSchoolDao;
import com.cn.ant.modules.score.entity.LqSchool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 学校录取线Service
 * @author AntDream
 * @version 2015-08-20
 */
@Service
@Transactional(readOnly = true)
public class LqSchoolService extends BaseService {

    @Autowired
	private LqSchoolDao lqSchoolDao;

	public LqSchool get(String id) {
		return lqSchoolDao.get(id);
	}
	
	public List<LqSchool> findList(LqSchool lqSchool) {
		return lqSchoolDao.findList(lqSchool);
	}

	public List<LqSchool> findBySchoolCode(String schoolCode, String schoolCodeSn) {
		LqSchool lqSchool = new LqSchool();
		lqSchool.setSchoolCode(schoolCode);
		lqSchool.setSchoolCodeSn(schoolCodeSn);
		return findList(lqSchool);
	}

	public int getCurrYear(){
		return lqSchoolDao.getCurrYear();
	}

	public Page<LqSchool> findPage(Page<LqSchool> page, LqSchool lqSchool) {
	    lqSchool.setPage(page);
        page.setList(lqSchoolDao.findList(lqSchool));
        return page;
	}
	
	@Transactional(readOnly = false)
	public void save(LqSchool lqSchool) {
	    if (lqSchool.getIsNewRecord()) {
            lqSchool.preInsert();
            lqSchoolDao.insert(lqSchool);
        } else {
            lqSchool.preUpdate();
            lqSchoolDao.update(lqSchool);
        }
	}

	@Transactional(readOnly = false)
	public void batchSave(List<LqSchool> list) {
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				list.get(i).preInsert();
				list.get(i).setId(Identities.generateUUID());
			}
			lqSchoolDao.batchInsert(list);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(LqSchool lqSchool) {
		lqSchoolDao.delete(lqSchool);
	}


	public int getLastTdx(Map<String, String> params) {
		Integer tdx = lqSchoolDao.getLastTdx(params);
		if (tdx == null) {
			return 0;
		}
		return tdx.intValue();
	}
}